package com.example.love_margin.Service.Impl;

import com.example.love_margin.Entity.Science;
import com.example.love_margin.Entity.Until.PageInfo;
import com.example.love_margin.Mapper.ScienceMapper;
import com.example.love_margin.Service.Impl.Until.delPetsUtil;
import com.example.love_margin.Service.ScienceService;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.forDelete;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ScienceServiceImpl implements ScienceService {

    @Autowired
    ScienceMapper scienceMapper;

    //批量删除实例化
    @Autowired
    delPetsUtil delP = new delPetsUtil();

    //创建删除批量文件的实例化
    @Autowired
    forDelete delete = new forDelete();

    @Override
    public Object addScience(Science science) {
        if(scienceMapper.addScience(science)==1){
            return ResultCode.SUCCESS.result("添加成功");
        }else {
            return ResultCode.SUCCESS.result("添加失败");
        }
    }

    @Override
    public Object deleteScience(int science_id) {

        if(scienceMapper.deleteScience(science_id)==1){
            return ResultCode.SUCCESS.result("删除成功");
        }else {
            return ResultCode.SUCCESS.result("删除失败");
        }
    }

    @Override
    public Object delScienceMany(Integer[] science_ids) {
        String sql = delP.deleSQL("science","science_id",science_ids);
        String URLs = delP.admin("pet_picture","science","science_id",science_ids);
        List<String> realUrls = scienceMapper.selectPetAll(URLs);
        if(delete.DeleteList(realUrls)) {
            if (scienceMapper.deleteScienceM(sql) >= 1) {
                return ResultCode.SUCCESS.result("删除成功");
            } else {
                return ResultCode.SUCCESS.result("删除失败");
            }
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object updateScience(Science science) {
        if(scienceMapper.upScience(science)==1){
            return ResultCode.SUCCESS.result( "修改成功");
        }else {
            return ResultCode.SUCCESS.result( "修改成功");
        }
    }

    @Override
    public Object selectScienceOne(int science_id) {

//        System.out.println(scienceMapper.selectOneScienceAll(science_id));
        return ResultCode.SUCCESS.result(scienceMapper.selectOneScienceAll(science_id));
    }

    @Override
    public Object selectScienceOne() {
        return ResultCode.SUCCESS.result(scienceMapper.selectTen());
    }

    @Override
    public Object selectScienPage(String science_id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Science> science = scienceMapper.selectScienceMany(science_id);
        // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
        PageInfo<Science> pageInfo = new PageInfo<>(science);
        return ResultCode.SUCCESS.result(pageInfo);
    }

}
